import * as Cesium from 'cesium/Cesium'
import 'cesium/Widgets/widgets.css'
export default class Track {
    constructor(config) {
        this.config = config
        this.trackEntity = null
    }

    init() {
        const { positions, id } = this.config
        // 启动时的时间
        const startTime = Cesium.JulianDate.fromDate(new Date())
        const endTime = Cesium.JulianDate.addSeconds(startTime, positions.length * 50, new Cesium.JulianDate())
        // 时钟设置
        let clock = viewer.clock
        clock.startTime = startTime.clone()
        clock.stop = endTime.clone()
        clock.currentTime = startTime.clone()
        clock.clockRange = Cesium.ClockRange.LOOP_STOP
        clock.multiplier = 2
        clock.shouldAnimate = true
        // 创建时间和位置坐标的关联关系
        const property = new Cesium.SampledPositionProperty()
        property.setInterpolationOptions({
            interpolationDegree: 1,
            interpolationAlgorithm: Cesium.HermitePolynomialApproximation
        })
        for (let i = 0; i < positions.length; i++) {
            // 该位置点时间
            const now = Cesium.JulianDate.addSeconds(startTime, i * 50, new Cesium.JulianDate())
            // 时间插值
            const position = Cesium.Cartesian3.fromDegrees(...positions[i])
            property.addSample(now, position)
        }
        this.createEntity(property, startTime, endTime, id)
    }

    createEntity(position, start, end, id) {
        this.trackEntity = new Cesium.Entity({
            availability: new Cesium.TimeIntervalCollection([
                new Cesium.TimeInterval({
                    start: start,
                    stop: end
                })
            ]),
            position,
            orientation: new Cesium.VelocityOrientationProperty(position),
            model: {
                uri: '../models/ship.glb', // 漫游模型
                minimumPixelSize: 100, // 模型大小
                maximumScale: 13000
            },
            path: {
                resolution: 1,
                material: new Cesium.PolylineGlowMaterialProperty({
                    color: Cesium.Color.fromCssColorString('#025C93'),
                    glowPower: 0.25,
                    taperPower: 1,
                    show: false
                }),
                width: 6
            },
            attribute: {
                id: id,
            }
        })
        viewer.entities.add(this.trackEntity)
    }

    destroy() {
        viewer.clock.shouldAnimate = false
        this.trackEntity && viewer.entities.remove(this.trackEntity)
    }
}





























